<?php
/**
 * Index.php
 * Niushop商城系统 - 团队十年电商经验汇集巨献!
 * =========================================================
 * Copy right 2015-2025 山西牛酷信息科技有限公司, 保留所有权利。
 * ----------------------------------------------
 * 官方网址: http://www.niushop.com.cn
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和使用。
 * 任何企业和个人不允许对程序代码以任何形式任何目的再发布。
 * =========================================================
 * @author : niuteam
 * @date : 2015.1.17
 * @version : v1.0.0.0
 */

namespace app\admin\controller;

use data\model\NsCouponGoodsModel;
use data\model\NsCouponTypeModel;
use data\model\NsGoodsModel;
use data\model\NsPromotionTuangouModel;
use data\service\Goods as GoodsService;
use data\service\Order as OrderService;
use data\service\User as User;
use data\service\Weixin;
use think\helper\Time;
use data\service\Member;
use data\service\TuangouApply;
use think\Db;

/**
 *
 *
 * @author Administrator
 *
 */
class Registration extends BaseController
{


    public function UserList()
    {

        if (request()->isAjax()) {
            $db = new TuangouApply();
            $condition = array();
            $page_index = request()->post('page_index', 1);
            $page_size = request()->post('page_size', PAGESIZE);
            $condition["shop_id"] = [
                [
                    "=",
                    $this->instance_id
                ]
            ];

            $data = $db->Userlist($page_index, $page_size, $condition);
            $coupon_type = new NsCouponTypeModel();
            $PromotionTuangou = new NsPromotionTuangouModel();
            $goods = new NsGoodsModel();
            $user = new User();
            foreach ($data['data'] as $k => $v) {
                $coupon_id = $PromotionTuangou->where(['tuangou_id' => $v['tuangou_id']])->value("coupon_id");
                $coupon_name = $coupon_type->where(['coupon_type_id' => $coupon_id])->value("coupon_name");
                $data['data'][$k]['coupon_id'] = $coupon_id;
                $data['data'][$k]['coupon_name'] = $coupon_name;
                $info = $user->getUserInfoDetail($v['uid']);
                $data['data'][$k]['user_name'] = $info['user_name'];
                $goods_data = $goods->where('goods_id', $v['goods_id'])->field('goods_name,goods_type_crowd')->find();
                $data['data'][$k]['goods_name'] = $goods_data['goods_name'];
                $data['data'][$k]['goods_type_crowd'] = $goods_data['goods_type_crowd'];
            }
            return $data;
        } else {
            return view($this->style . "Registration/couponTypeList");
        }

    }

    /**
     * 报名审核
     * @return array|\multitype
     */
    public function audit()
    {
        if (request()->isAjax()) {
            $db = new TuangouApply();
            $user = new Member();
            $db->apply->startTrans();
            try {
                $tuangou_apply_id = request()->post('tuangou_apply_id');
                $uid = request()->post('uid');
                $coupon_type_id = request()->post('coupon_type_id');
                $is_open = request()->post('is_open');


                $res = $db->audit($tuangou_apply_id, $is_open);
                if($res<1){
                    $db->apply->rollback();
                    return AjaxReturn($res);
                }
                if ($res && $is_open==1) {
                    $boole = $user->memberGetCoupon($uid, $coupon_type_id, 2);
//                  if($boole<1){
//                      $db->apply->rollback();
//                      return AjaxReturn($boole);
//                  }
                }
                // 提交事务
                $db->apply->commit();
                return AjaxReturn($res);
            } catch (\Exception $e) {
                // 回滚事务
                $db->apply->rollback();
                return ['code' => 0, 'message' => '数据异常'];
            }
        }
    }
}
